$(document).ready(function () {
    $('#upper-status-menu').tabs();
    Mockingbird.BindEvents();
});

Mockingbird = {
    BindEvents: function () {
        $(document).ajaxError(function (event, request, settings) {
            alert("Sorry, an error occured!");
        });

        $(document).on('keypress', 'div.place-comment div.comment-text', function (k) {
            if (k.shiftKey && k.keyCode == 13) {
                console.log("aa");
                var elem = $('div.place-comment div.comment-text textarea');
                var elemH = elem.height();
                var fontSize = elem.css('font-size');
                var lineHeight = Math.floor(parseInt(fontSize.replace('px', '')) * 1.75);
                elem.height(elemH + lineHeight);
            }
            else {
                if (k.keyCode == 13) {
                    k.preventDefault();
                    console.log("try prevent");
                }
            }
        });

        $(".main-stream").height($(window).height());

        $(document).on("click", ".followUser", function () {
            var li = $(this);
            var id = li.attr("follow-id");

            $.ajax({
                url: Site.getPathToActionMethod("Follow", "User"),
                data: { userId: id },
                success: function (data) {
                    li.removeClass("followUser");
                    li.addClass("unfollowUser");
                    li.find("i").text("Unfollow");
                },
                error: function (data) {
                    alert("An error has occured");
                },
                async: true,
                type: "POST"
            });
        });

        $(document).on("click", ".unfollowUser", function () {
            var li = $(this);
            var id = li.attr("follow-id");

            $.ajax({
                url: Site.getPathToActionMethod("Unfollow", "User"),
                data: { userId: id },
                success: function (data) {
                    li.removeClass("unfollowUser");
                    li.addClass("followUser");
                    li.find("i").text("Follow");
                },
                error: function (data) {
                    alert("An error has occured");
                },
                async: true,
                type: "POST"
            });
        });

        $(document).on("click", "#ShowFriends", function (e) {
            $.ajax({
                url: Site.getPathToActionMethod("GetFriends", "User"),
                success: function (data) {
                    $("#main-stream").html(data);
                    $("#ShowFriends").addClass("active");
                    $("#ShowFeed").removeClass("active");
                },
                error: function (data) {
                    alert("An error has occured");
                },
                async: true,
                type: "GET"
            });

            e.preventDefault();
        });

        $(document).on("click", "#ShowFeed", function () {
            window.location = Site.getPathToActionMethod("Index", "NewsFeed");
        });

        setInterval(function () {
            if ($(".stream-container").length > 0 && $("#useHeartBeat").length > 0) {
                $.ajax({
                    url: Site.getPathToActionMethod("GetNewsFeedForUserHeartbeat", "NewsFeed"),
                    success: function (data) {
                        $(".stream-container").replaceWith(data);
                    },
                    cache: false,
                    async: true,
                    type: "GET"
                });
            }
        }, 30000);

        //textarea limit
        $(document).on('keyup', '#status-textarea', function () {
            $('span#sym-left').text(parseInt(140 - $('#status-textarea').val().length));
            if ($('span#sym-left').text() == 0) {
                $('span#sym-left').css('color', 'red');
            } else {
                $('span#sym-left').css('color', 'black');
            }
        });

        //clear button
        $(document).on('click', '#clear-search', function () {
            $('#search-query').val("");
        });
    }
};
